package org.eduapp.trains.utils;

import java.awt.Canvas;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.AreaAveragingScaleFilter;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.io.File;

import javax.imageio.ImageIO;

/**
 * <p>Title: ZoomImage</p>
 * <p>Description: </p>
 *
 * @author Yaodi
 * @version 1.0
 */
public class ZoomImage {

	private static Component component = new Canvas();

	private static String[] imageFormatArray = new String[] { ".jpg", ".jpeg", ".gif", ".png", ".bmp" };//pcx/tga/tif这三种格式目前还不支持

	/**  
	 * 校验图像文件的格式是否可以进行缩放  
	 */
	public boolean verifyImageFormat(String fileName) {
		boolean result = false;
		for (int i = 0; i < imageFormatArray.length; i++) {
			if (fileName.toLowerCase().lastIndexOf(imageFormatArray[i]) == (fileName
					.length() - imageFormatArray[i].length())) {
				result = true;
				break;
			}
		}
		return result;
	}

	
	/**  
	 * 将目录下的所有图像进行放大缩小  
	 *   
	 * @param strDir 图像的目录  
	 * @param zoomRatio 放大缩小的倍率  
	 * @param rebuild 是否重新创建，即已经存在的图像是否覆盖重建  
	 * @throws Exception  
	 */
	public void zoom(String strDir, double zoomRatio, boolean rebuild)
			throws Exception {
		File fileDir = new File(strDir);
		if (!fileDir.exists()) {
			return;
		}
		String dirTarget = strDir + "/small" + zoomRatio;
		File fileTarget = new File(dirTarget);
		if (!fileTarget.exists()) {
			fileTarget.mkdir();
		}
		File[] files = fileDir.listFiles();
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < files.length; i++) {
			String fileFullName = files[i].getCanonicalPath();
			String fileShortName = files[i].getName();
			if (!new File(fileFullName).isDirectory())// 排除二级目录，如果想就再递归一次，这里省略   
			{
				if (verifyImageFormat(fileShortName)) {

					stringBuilder = new StringBuilder();
					stringBuilder.append(dirTarget).append("/").append(
							fileShortName);
					if (!new File(stringBuilder.toString()).exists() || rebuild) {
						try {
							createZoomSizeImage(fileFullName, stringBuilder
									.toString(), zoomRatio);
						} catch (Exception e) {

						}
					}
				}
			}
		}
	}

	
	/**  
	 * 按比例进行放大或缩小图像，zoomRatio = 1为原大，zoomRatio > 1为放大，zoomRatio < 1 为缩小  
	 */
	public static void createZoomSizeImage(String fileName, String fileNameTarget,
			double zoomRatio) throws Exception {
		
		String toDir = fileNameTarget.substring(0,fileNameTarget.lastIndexOf(System.getProperty("file.separator")));
		File dirFile = new File(toDir);
		if(!dirFile.exists()){
			dirFile.mkdirs();
		}
		
		Image image = ImageIO.read(new File(fileName));
		int width = new Double(image.getWidth(null) * zoomRatio).intValue();
		int height = new Double(image.getHeight(null) * zoomRatio).intValue();
		AreaAveragingScaleFilter areaAveragingScaleFilter = new AreaAveragingScaleFilter(
				width, height);
		FilteredImageSource filteredImageSource = new FilteredImageSource(image
				.getSource(), areaAveragingScaleFilter);
		BufferedImage bufferedImage = new BufferedImage(width, height,
				BufferedImage.TYPE_3BYTE_BGR);
		Graphics graphics = bufferedImage.createGraphics();
		graphics.drawImage(component.createImage(filteredImageSource), 0, 0,
				null);
		ImageIO.write(bufferedImage, "JPEG", new File(fileNameTarget));
	}
	
	/**
	 * 
	 * @param fileName
	 * @param fileNameTarget
	 * @param zoomWidth
	 * @throws Exception
	 * @author Zhao.Xiang
	 */
	public static void createZoomSizeImageByWidth(String fileName, String fileNameTarget,
			int zoomWidth) throws Exception {
		
		String toDir = fileNameTarget.substring(0,fileNameTarget.lastIndexOf(System.getProperty("file.separator")));
		File dirFile = new File(toDir);
		if(!dirFile.exists()){
			dirFile.mkdirs();
		}
		
		Image image = ImageIO.read(new File(fileName));
		double zoomRatio = zoomWidth >= image.getWidth(null) ? 1 : (100 * zoomWidth / image.getWidth(null)) * 0.01;
		int width = zoomWidth;
		int height = new Double(image.getHeight(null) * zoomRatio).intValue();
		AreaAveragingScaleFilter areaAveragingScaleFilter = new AreaAveragingScaleFilter(
				width, height);
		FilteredImageSource filteredImageSource = new FilteredImageSource(image
				.getSource(), areaAveragingScaleFilter);
		BufferedImage bufferedImage = new BufferedImage(width, height,
				BufferedImage.TYPE_3BYTE_BGR);
		Graphics graphics = bufferedImage.createGraphics();
		graphics.drawImage(component.createImage(filteredImageSource), 0, 0,
				null);
		ImageIO.write(bufferedImage, "JPEG", new File(fileNameTarget));
	}
	
	/**
	 * 
	 * @param fileName
	 * @param fileNameTarget
	 * @param zoomHeight
	 * @throws Exception
	 * @author Zhao.Xiang
	 */
	public static void createZoomSizeImageByHeight(String fileName, String fileNameTarget,
			int zoomHeight) throws Exception {
		
		String toDir = fileNameTarget.substring(0,fileNameTarget.lastIndexOf(System.getProperty("file.separator")));
		File dirFile = new File(toDir);
		if(!dirFile.exists()){
			dirFile.mkdirs();
		}
		
		Image image = ImageIO.read(new File(fileName));
		double zoomRatio = zoomHeight >= image.getHeight(null) ? 1 : (100* zoomHeight / image.getHeight(null)) * 0.01;
		zoomRatio = zoomRatio > 1 ? 1 : zoomRatio;
		int width = new Double(image.getWidth(null) * zoomRatio).intValue();
		int height = zoomHeight;
		AreaAveragingScaleFilter areaAveragingScaleFilter = new AreaAveragingScaleFilter(
				width, height);
		FilteredImageSource filteredImageSource = new FilteredImageSource(image
				.getSource(), areaAveragingScaleFilter);
		BufferedImage bufferedImage = new BufferedImage(width, height,
				BufferedImage.TYPE_3BYTE_BGR);
		Graphics graphics = bufferedImage.createGraphics();
		graphics.drawImage(component.createImage(filteredImageSource), 0, 0,
				null);
		ImageIO.write(bufferedImage, "JPEG", new File(fileNameTarget));
	}
	
	public static void main(String[] args) throws Exception {
		ZoomImage.createZoomSizeImageByWidth(
				"D:\\x36zhao\\Personal\\Photos\\WallPaper\\3d02.jpg", "D:\\x36zhao\\Desktop\\0.25.jpg", 820);// 缩小2倍   		
//		ZoomImage.createZoomSizeImage(
//				"D:\\x36zhao\\Personal\\Photos\\Headers\\1001131146b13a092bf24afc2a.jpg", "D:\\x36zhao\\Desktop\\0.5.jpg", 0.5);// 缩小2倍   		
//		ZoomImage.createZoomSizeImage(
//				"D:\\x36zhao\\Personal\\Photos\\Headers\\1001131146b13a092bf24afc2a.jpg", "D:\\x36zhao\\Desktop\\0.75.jpg", 0.75);// 缩小2倍   		

		//zoomImage.zoom("F:/pic",2,true);// 放大2倍   
		System.out.println("************THEEND************");
	}
}

